window.onload=function(){
    changeRandomCodeImg();
}

var Cookie = {
    set: function (key, value, exdays) {
        let exdate = new Date() // 获取时间
        exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // 保存的天数
        // 字符串拼接cookie
        // eslint-disable-next-line camelcase
        window.document.cookie = key + '=' + value + ';path=/;expires=' + exdate.toGMTString()
    },
    get: function (key) {
        if (document.cookie.length > 0) {
            let arr = document.cookie.split('; ') // 这里显示的格式需要切割一下自己可输出看下
            for (let i = 0; i < arr.length; i++) {
                let arr2 = arr[i].split('=') // 再次切割
                // 判断查找相对应的值
                if (arr2[0] === key) {
                    return arr2[1]
                }
            }
            return "";
        }
        return "";
    },
    remove: function (key) {
        this.set(key, '', -1);
    }
};

$(function () {
    $('.checkbox').click(function (event) {
        if ($(this).hasClass('checked')) {
            $(this).removeClass('checked');
            //忘记密码
            console.log("忘记我！");
            Cookie.remove("ident.aomall.com");
            Cookie.remove("password.aomall.com");
        } else {
            $(this).addClass('checked');
            //记住密码
            console.log("记住我！");
            Cookie.set("ident.aomall.com", document.querySelector("[name='userName']").value, 7);
            Cookie.set("password.aomall.com", document.querySelector("[name='passWord']").value, 7);
        }
    });
    $('input').focusin(function (event) {
        $(this).css('borderColor', '#0079CC');
    });
    $('input').focusout(function (event) {
        $(this).css('borderColor', '#cacaca');
    });
});

layui.use('layer', function () {
    var layer = layui.layer;
});

document.querySelector("[name='ident']").value = Cookie.get("ident.aomall.com");
document.querySelector("[name='passWord']").value = Cookie.get("password.aomall.com");

function doLogin() {
    let collectData={};
    let ident = document.querySelector("[name='userName']").value;
    let password = document.querySelector("[name='passWord']").value;
    let randomCode = document.querySelector("body > div > div > div > div.MCup.clearfix > div.MCright.W310 > p.clearfix.yzm > input[type=text]").value;
    console.log("电话/邮箱：" + ident);
    console.log("密码：" + password);
    collectData.ident = ident;
    collectData.passWord = password;
    collectData.randomCode = randomCode;
    $.ajax({
        url: "/users/login?ident=" + ident + '&passWord=' + password,
        type: "POST",
        data: JSON.stringify(collectData),
        dataType: "text",
        success: function (resp) {
            let result = JSON.parse(resp);//字符串转成JSON对象
            console.log(result);
            if (result.code === 200) {
                console.log("登录成功的对象是：");
                //把服务器返回的登陆成功的用户对象保存到服务器缓存里面
                localStorage.setItem("currentLoginUserPhone.aomall.com", result.data.userPhone);
                if (result.data.userName === null||result.data.userName==="") {
                    localStorage.setItem("currentLoginUser.aomall.com", result.data.userPhone);
                    layer.msg('登录成功，欢迎您 ' + result.data.userPhone + ' !');
                } else {
                    localStorage.setItem("currentLoginUser.aomall.com", result.data.userName);
                    layer.msg('登录成功，欢迎您 ' + result.data.userName + ' !');
                }
                //跳转到主页
                window.setTimeout("javascript:window.location.href = 'index.html'", 1000);
            } else {
                localStorage.setItem("currentLoginUser.aomall.com", "");
                layer.msg(result.msg);
            }
        }
    });
}

function changeRandomCodeImg() {
    //document.querySelector("#randomCodeImg")
    $("#randomCodeImg").prop("src","WebContent/user/commons/randomCodeImg?tm"+new Date().getMilliseconds())
}